package com.newland.esp.controller.config;

import com.newland.esp.controller.intercepter.CrosInterceptor;
import com.newland.esp.controller.intercepter.ManagerLoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @author lw
 * @descripe 拦截器前后端分离不太会使用
 * @date 2021/8/14 20:08
 * @Version 1.0
 **/
@Configuration
public class MyInterceptorConfig extends WebMvcConfigurationSupport {

    //关键，将拦截器作为bean写入配置中 这样拦截器才能放到容器中
    @Bean
    public ManagerLoginInterceptor managerLoginInterceptor() {
        return new ManagerLoginInterceptor();
    }

    @Bean
    public CrosInterceptor crosInterceptor() {return new CrosInterceptor();}

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 放行路径
        List<String> patterns = new ArrayList();
        patterns.add("/webjars/**");
        patterns.add("/druid/**");
        patterns.add("/sys/login");
        patterns.add("/swagger/**");
        patterns.add("/v2/**");
        patterns.add("/swagger-ui.html");
        patterns.add("/swagger-ui.html/**");
        patterns.add("/swagger-resources/**");
        patterns.add("/login/**");
        patterns.add("/user/login/**");
        patterns.add("/user/logout/**");
        patterns.add("/image/**");



        // 将上面自定义好的拦截器添加进去。
        registry.addInterceptor(crosInterceptor()).addPathPatterns("/**").excludePathPatterns(patterns);
        registry.addInterceptor(managerLoginInterceptor()).addPathPatterns("/**").excludePathPatterns(patterns);

        super.addInterceptors(registry);
    }
}

//参考博客：https://blog.csdn.net/Herishwater/article/details/103544342
